package org.action.news;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.action.BaseAction;
import org.biz.news.IColumnBiz;
import org.hibernate.entity.Column;
import org.hibernate.entity.NewsTitle;
import org.hibernate.entity.Suggestion;

import common.util.page.WebPagination;

/**
 * 
 * 新闻栏目频道控制器
 * 
 * @Title: ColumnAction.java
 * @Description:新闻系统(一期)
 * @Copyright: Copyright (c) 2011
 * @Company:北京学邦时代
 * @author 苗有虎
 * @date Oct 30, 2012 11:40:37 AM
 * @version V4.0
 */
public class ColumnAction extends BaseAction {

	private static final long serialVersionUID = -1083855769484289271L;

	private IColumnBiz columnBiz = null;

	private WebPagination page = new WebPagination(); // 分页对象

	private Column column = new Column(); // 栏目对象

	/** (20131223 add by xy) start... */
	/** 栏目id */
	private String strColumnsID = ",";

	/** 推荐列表条数限制 */
	//private Integer intRecommendLimit = 5;
	/** 推荐列表 */
	private List<NewsTitle> lstRecommendNews = new ArrayList<NewsTitle>();

	/** 咨询列表 */
	//private Integer intConsultingLimit = 5;
	/** 周排行 */
	private List<NewsTitle> lstWeekClicks = new ArrayList<NewsTitle>();
	/** 天（48h）排行 */
	private List<NewsTitle> lstDaysClicks = new ArrayList<NewsTitle>();
	/** 子栏目 */
	private List<Column> lstChannelColumns = new ArrayList<Column>();
	
	//private Map<Column,List<Column>> parentColumns = new HashMap<Column,List<Column>>();
	/** 最新文章 */
	private Integer intArticlesLimit = 5;
	/** (20131223 add by xy) end... */

	/** 20140127(增加三级栏目add by xy) start... */
	/** 对于没有三级栏目的栏目来说，此属性可以忽略 */
	private Integer levelOfColumn = null;
	private boolean hasChildColumn = false;
	private Integer intNavChoiceID = null;
	private Integer intColChoiceID = null;
	// 有3级的2级栏目文章展示限制
	private Integer int2LevelLimit = 7;
	private Integer int3LevelLimit = 5;

	/** 20140127(增加三级栏目add by xy) end... */

	public Column getColumn() {
		return column;
	}

	public void setColumn(Column column) {
		this.column = column;
	}

	public WebPagination getPage() {
		return page;
	}

	public void setPage(WebPagination page) {
		this.page = page;
	}

	public IColumnBiz getColumnBiz() {
		return columnBiz;
	}

	public void setColumnBiz(IColumnBiz columnBiz) {
		this.columnBiz = columnBiz;
	}

	/**
	 * @return the strColumnsID
	 */
	public String getStrColumnsID() {
		return strColumnsID;
	}

	/**
	 * @param strColumnsID
	 *            the strColumnsID to set
	 */
	public void setStrColumnsID(String strColumnsID) {
		this.strColumnsID = strColumnsID;
	}

	/**
	 * @return the lstRecommendNews
	 */
	public List<NewsTitle> getLstRecommendNews() {
		return lstRecommendNews;
	}

	/**
	 * @param lstRecommendNews
	 *            the lstRecommendNews to set
	 */
	public void setLstRecommendNews(List<NewsTitle> lstRecommendNews) {
		this.lstRecommendNews = lstRecommendNews;
	}

	/**
	 * @return the lstWeekClicks
	 */
	public List<NewsTitle> getLstWeekClicks() {
		return lstWeekClicks;
	}

	/**
	 * @param lstWeekClicks
	 *            the lstWeekClicks to set
	 */
	public void setLstWeekClicks(List<NewsTitle> lstWeekClicks) {
		this.lstWeekClicks = lstWeekClicks;
	}

	/**
	 * @return the lstDaysClicks
	 */
	public List<NewsTitle> getLstDaysClicks() {
		return lstDaysClicks;
	}

	/**
	 * @param lstDaysClicks
	 *            the lstDaysClicks to set
	 */
	public void setLstDaysClicks(List<NewsTitle> lstDaysClicks) {
		this.lstDaysClicks = lstDaysClicks;
	}

	/**
	 * @return the lstChannelColumns
	 */
	public List<Column> getLstChannelColumns() {
		return lstChannelColumns;
	}

	/**
	 * @param lstChannelColumns
	 *            the lstChannelColumns to set
	 */
	public void setLstChannelColumns(List<Column> lstChannelColumns) {
		this.lstChannelColumns = lstChannelColumns;
	}
	
	/**
	 * 新闻列表页数据
	 * 
	 * @return
	 */
	public String toList() {
		//初始化菜单
		//initMenu(null);
		// 列表数据
		this.columnList(false);
		// // 相关标签;
		// this.hotTag();
		// // 本栏目相关热门文章
		// hotArticle();
		
		return "toList";
	}

	public String toPages() {
		//初始化菜单
		//initMenu(null);
		// 列表数据
		this.columnList(true);
		return "toPages";
	}
	public String toQuestion() {
		// 列表数据
		this.suggestionList();
		return "toQuestion";
	}
	/**
	 * 互动答疑
	 */
	public void suggestionList() {
		this.column = (Column) this.columnBiz.selObjectById(Column.class,this.column.getId());
		
		this.page.setCount(this.columnBiz.doCountQueAns());
		this.page.setPageSize(this.column.getListPagesNumber());
		Map<Column, List<Suggestion>> mapSuggestion = new LinkedHashMap<Column, List<Suggestion>>();
		mapSuggestion.put(column, columnBiz.selByQueAns(column.getId(),this.page.getPageNo(), this.page.getPageSize()));
		this.set("mapSug", mapSuggestion);
		this.page.setUrl(request.getContextPath() + "/question/"+ this.column.getId());
		this.set("page_style", this.page.newsPros());
		if(column.getMenuColumns()!=null){
			this.set("navFist", this.columnBiz.findByCidCol(column.getMenuColumns().getId()));			
			this.set("navSec", column);
		}else{
			this.set("navFist", column);
		}
		this.initMenu();
	}
	/**
	 * 本栏目的列表的内容;
	 */
	public void columnList(boolean flag) {
		this.column = (Column) this.columnBiz.selObjectById(Column.class,this.column.getId());
		if (column != null) {
			// 栏目选择
			intColChoiceID = column.getId();
			this.set("column", column);
		}
		if (column != null) {
			//gainColumnsID(column);
			strColumnsID = column.getId().toString();
			this.page.setCount(this.columnBiz.doCountNewsByColumn(strColumnsID));//this.column.getId()
			this.page.setPageSize(this.column.getListPagesNumber());
			this.set("lists",this.columnBiz.findColumnNews(strColumnsID,this.page.getPageNo(), this.page.getPageSize()));
			
			intNavChoiceID = column.getId();
		}
	
		/*if(intNavChoiceID == intColChoiceID){
			this.set("navFist", this.columnBiz.findByCidCol(intNavChoiceID));
		}else{
			this.set("navFist", this.columnBiz.findByCidCol(intNavChoiceID));
			this.set("navSec", this.columnBiz.findByCidCol(intColChoiceID));
		}*/
		if(column.getMenuColumns()!=null){
			this.set("navFist", this.columnBiz.findByCidCol(column.getMenuColumns().getId()));			
			this.set("navSec", column);
		}else{
			this.set("navFist", column);
		}
		this.set("menuLevel", levelOfColumn);//菜单级别
		this.set("hasChildMenu", hasChildColumn);//是否有子菜单
		
		this.initMenu();
		if(flag){
			this.page.setUrl(request.getContextPath() + "/column/"+ this.column.getId());
			this.set("page_style", this.page.newsPros());
		}else{
			this.page.setUrl(request.getContextPath() + "/channel/"+ this.column.getId());
			this.set("page_style", this.page.newsPros());
		}
		
		
	}

	/**
	 * 本栏目相关热门文章
	 */
	public void hotArticle() {
		// 无图
		this.set("hotArticleNoPic",
				this.columnBiz.selHotArticleByCid(this.column.getId(), 10));
		// 有图
		this.set("hotArticlePic",
				this.columnBiz.selHotArticleByCidPic(this.column.getId()));
	}

	/** 　20131223 add by xy start... */
	/**
	 * 获得其子孙栏目的ID 在这里要进行甄别操作，不能出现其子类是其父类（这是错误操作导致的结果）的情况
	 */
	private void gainColumnsID(Column menuColumns) {

		if (menuColumns != null) {
			List<Column> lstTempMenuColumns = this.columnBiz.listChildColumns(menuColumns);
			// 先判断此时id是否已经出现在字符串内部，如果出现，则表明栏目有问题
			if (!strColumnsID.contains("," + menuColumns.getId().intValue()+ ",")) {
				strColumnsID += menuColumns.getId() + ",";// 栏目id以逗号分隔开
				if (lstTempMenuColumns != null
						&& lstTempMenuColumns.size() != 0) {
					for (Column tempMenuColumn : lstTempMenuColumns) {
						gainColumnsID(tempMenuColumn);
					}
				}
			} 
		}
	}

	/**
	 * 展示最新文章
	 */
/*	private void initNewsetArticle() {

		// 最新文章，限定5篇
		List<NewsTitle> lstArticles = this.columnBiz
				.listNewestArciles(intArticlesLimit);
		this.set("lstArticles", lstArticles);
	}*/

	/**
	 * 检查栏目的级别
	 * 
	 * @param column
	 *            Column 栏目
	 */
	private void checkColumnLevel() {

		// 栏目ID
		int intChoiceID = column.getId();
		// 父栏目列表(防止错误将某个上级栏目的上级设为自己，则会出现死循环)
		String strParentID = ",";
		// 防止死循环(目前最高级别是3级)
		int intFloor = 0;
		Column tmpColumn = column;
		while (tmpColumn.getMenuColumns() != null && intFloor <= 4) {
			strParentID += tmpColumn.getMenuColumns().getId() + ",";
			intFloor++;
			if (strParentID.contains("," + intChoiceID + ",")) {
				System.out.println("ERROR！错误栏目ID:" + column.getId());
				// 错误级别设置为4
				intFloor = 4;
				break;
			}
			tmpColumn = (Column) columnBiz.selObjectById(Column.class,
					tmpColumn.getMenuColumns().getId());
		}
		// 如果出错，不处理
		if (intFloor < 4) {
			this.levelOfColumn = intFloor + 1;
			this.set("navChoice", tmpColumn);
			if(intFloor ==1 ){
				this.set("colChoice",column);
			} else if (intFloor ==2){
				this.set("colChoice",this.column.getMenuColumns());
			}else if (intFloor ==3){
				
			}
		}
	}

	/**
	 * 初始化2级栏目
	 * 
	 */
	private void init2Level(List<Column> lstColumn) {

		Map<Column, List<NewsTitle>> mapNewsTitle = new LinkedHashMap<Column, List<NewsTitle>>();
		for (Column col : lstColumn) {
			mapNewsTitle.put(col,columnBiz.selByColumn(col.getId(), 1, int2LevelLimit));
		}
		//this.set("lists", mapNewsTitle);
		this.request.getSession().setAttribute("lists", mapNewsTitle);
	}

	/**
	 * 初始化3级栏目
	 * 
	 */
	private void init3Level(boolean flag) {

		this.page.setCount(this.columnBiz.doCountNewsByColumn(this.column.getId()));
		// 每页显示的条数
		this.page.setPageSize(int3LevelLimit);
		Map<Column, List<NewsTitle>> mapNewsTitle = new LinkedHashMap<Column, List<NewsTitle>>();
		mapNewsTitle.put(column, columnBiz.selByColumn(column.getId(),this.page.getPageNo(), int3LevelLimit));
		this.set("lists", mapNewsTitle);
		if(flag){
			this.page.setUrl(request.getContextPath() + "/column/"+ this.column.getId());
		}else{
			this.page.setUrl(request.getContextPath() + "/channel/"+ this.column.getId());
		}
		this.set("page_style", this.page.newsPros());
		boolean hasPaging = false;
		if(this.page.getCount()>int3LevelLimit){
			hasPaging = true;
		}
		if(intNavChoiceID == intColChoiceID){
			this.set("navFist", this.columnBiz.findByCidCol(intNavChoiceID));
		}else{
			this.set("navFist", this.columnBiz.findByCidCol(intNavChoiceID));
			this.set("navSec", this.columnBiz.findByCidCol(intColChoiceID));
		}
		this.set("menuLevel", levelOfColumn);//菜单级别
		this.set("hasChildMenu", hasChildColumn);//是否有子菜单
		// 添加栏目
		lstChannelColumns = this.columnBiz.listChildColumns(column.getMenuColumns());
		//this.set("hasPaging", hasPaging);
		this.request.getSession().setAttribute("hasPaging", hasPaging);
	}
	/**
	 * 初始化菜单
	 * @param i
	 * @param parent_Menu
	 */
	@SuppressWarnings("unused")
	private void initMenu(Column parent_Menu) {
		if(parent_Menu==null){//一级栏目,获取子菜单
			List<Column> menuColumns1 = this.columnBiz.listChildColumns(parent_Menu);
			for(int i=0;i<menuColumns1.size();i++){
				List<Column> menuColumns2 = this.columnBiz.listChildColumns(menuColumns1.get(i));
				for(int j=0;j<menuColumns2.size();j++){
					List<Column> menuColumns3 = this.columnBiz.listChildColumns(menuColumns2.get(j));
					for(int m=0;m<menuColumns3.size();m++){
						List<Column> menuColumns4 = this.columnBiz.listChildColumns(menuColumns3.get(m));
					}
				}
			}
		}
	}
	/**
	 * 初始化菜单
	 */
	private void initMenu(){
		lstChannelColumns=null;
		lstChannelColumns=this.columnBiz.listChildColumns();
		List<Column> towColumns=new ArrayList();
		List<Column> threeColumns=new ArrayList();
		for(Column fc:lstChannelColumns){
			fc.setParentId(fc.getMenuColumns()==null?null:fc.getMenuColumns().getId());

			if(fc.getParentId()!=null){
				for(Column fcc:lstChannelColumns){
					if(fcc.getId().equals(fc.getParentId())){
						if(fcc.getParentId()==null){
							towColumns.add(fc); 
							break;
						}else{
							threeColumns.add(fc);break;
						}
					}
				}
			}
				
		}
		for(Column fc:lstChannelColumns){
			for(Column towfc:towColumns){
				if(fc.getId().equals(towfc.getParentId())){
					fc.ischi=true;
				}
			}
		}
		this.set("towColumns", towColumns);
		this.set("threeColumns", threeColumns);
	}
	/**
	 * 加载二级栏目
	 * @author 刘丽娜
	 * 20160509
	 * @return
	 */
	public String toNavSec() {
		this.column = (Column) this.columnBiz.selObjectById(Column.class,this.column.getId());
		List<Column> ChannelColumns = null;//1级子栏目
		Map<Column, List<NewsTitle>> mapChannel = new LinkedHashMap<Column, List<NewsTitle>>();
		if (column != null) {
			ChannelColumns = this.columnBiz.listChildColumns(column);
			for(int i=0;i<ChannelColumns.size();i++){
				List<Column> ChannelColumns2 = null;//2级子栏目
				ChannelColumns2 = this.columnBiz.listChildColumns(ChannelColumns.get(i));
				if(ChannelColumns2!=null && ChannelColumns2.size()>0){//存在3级栏目（即2级子栏目）
					mapChannel.put(ChannelColumns.get(i), columnBiz.selByColumn(ChannelColumns2.get(0).getId(),1, 8));
				}else{
					mapChannel.put(ChannelColumns.get(i), columnBiz.selByColumn(ChannelColumns.get(i).getId(),1, 8));
				}
			}
		}
		this.set("mapNavSec", mapChannel);
		this.set("navFist", column);
		//加载菜单
		this.initMenu();
		return "toNavSec";
	}
}